//
//  HLTmyuserinfoTableViewController.m
//  huiliantong
//
//  Created by hlt on 15/11/13.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTmyuserinfoTableViewController.h"



@interface HLTmyuserinfoTableViewController ()

@property (weak, nonatomic) IBOutlet UILabel *email;

@property (weak, nonatomic) IBOutlet UILabel *qq;

@property (weak, nonatomic) IBOutlet UILabel *name;


@property (weak, nonatomic) IBOutlet UILabel *usercode;


@property (weak, nonatomic) IBOutlet UILabel *sex;



@end

@implementation HLTmyuserinfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.view.center;
    [indicatorView startAnimating];
    [self.view addSubview:indicatorView];
    
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.navigationItem setTitle:@"个人资料"];
    
    
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    

    
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    NSString *url=[chaxunuserinfo stringByAppendingFormat:@"&username=%@",acc.custPhone];
    [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
        
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
        self.email.text=ary[0][@"email"];
        //        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        //        NSString *str = [numberFormatter stringFromNumber:ary[0][@"qq"]];
        if ([ary[0][@"qq"] isEqual:@"0"]||[ary[0][@"qq"] isEqual:@""]) {
            self.qq.text=@"未设置";
            
        }else
        {
            self.qq.text=ary[0][@"qq"];
        }
        
        
        
        if ([ary[0][@"name"] isEqual:[NSNull null]]) {
            self.name.text=@"未设置";
        }
        else
        {
            self.name.text=ary[0][@"name"];
            
        }
        
        if([ary[0][@"id_card"] isEqual:[NSNull null]])
        {
            self.usercode.text=@"未设置";
        }
        else
        {
            self.usercode.text=ary[0][@"id_card"];
        }
        
        NSString *sex;
        if ([ary[0][@"sex"] isEqual:@"1"]) {
            
            sex=@"男";
        }else
        {
            sex=@"女";
        }
        self.sex.text=sex;
        
        
    } faliure:^(NSString *error) {
        
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];
        
    }];
    
    
    //添加退出按钮
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    searchButton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [searchButton setTitle:@"退出" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(usertuichu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    //     self.navigationItem.rightBarButtonItems = @[searchItem,moreItem];
    self.navigationItem.rightBarButtonItem = searchItem;
    
}


-(void) usertuichu
{
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    //退出登录
    [[AccountTool sharedAccountTool] tuichu:acc];
    
    [self viewWillDisappear:YES];
    [self viewWillAppear:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            
            [EYInputPopupView popViewWithTitle:@"设置Email" contentText:self.email.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.email.text=text;
                                   } dismissBlock:^{
                                       
                                   }];
            
        }else if (indexPath.row==1)
        {
            [EYInputPopupView popViewWithTitle:@"设置QQ" contentText:self.qq.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.qq.text=text;
                                   } dismissBlock:^{
                                       
                                   }];
        }else if (indexPath.row==2)
        {
            [EYInputPopupView popViewWithTitle:@"设置姓名" contentText:self.name.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.name.text=text;
                                   } dismissBlock:^{
                                       
                                   }];
        }else if (indexPath.row==3)
        {
            [EYInputPopupView popViewWithTitle:@"设置身份证号" contentText:self.usercode.text
                                          type:EYInputPopupView_Type_single_line_text
                                   cancelBlock:^{
                                       
                                   } confirmBlock:^(UIView *view, NSString *text) {
                                       
                                       self.usercode.text=text;
                                   } dismissBlock:^{
                                       
                                   }];
        }
        else if (indexPath.row==4)
        {
            
            NSArray *tags=@[@"男",@"女"];
            
            [EYTagPopupView popViewWithTitle:@"设置性别"
                                        tags:tags
                                        type:EYTagPopupView_Type_Single_Selected
                                 cancelBlock:^{
                                     
                                 } confirmBlock:^(UIView *view, NSArray *tags) {
                                     if ([tags count]>0) {
                                         
                                         self.sex.text=tags[0];
                                     }
                                 } dismissBlock:^{
                                     
                                 }];
            
        }
        
        
        
    }
    
    
    
    
    
}







- (IBAction)tijiao:(id)sender {
    
    
    
    
    
    NSString *sex;
    if ([self.sex.text isEqual:@"男"]) {
        sex=@"1";
        
    }else
    {
        sex=@"2";
    }
    
    NSString *id_card;
    if ([self.usercode.text isEqual:@"未设置"]) {
        
        id_card=@"";
    }else
    {
        id_card=self.usercode.text;
    }
    
    
    NSString *qq;
    if ([self.qq.text isEqual:@"未设置"]) {
        
        qq=@"";
    }else
    {
        qq=self.qq.text;
    }
    
    
    if ([id_card length]!=18) {
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"身份证号码填写不对，请重新填写" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
        [alert show];
    }
    else
    {
        
        UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
        view1.backgroundColor=[UIColor grayColor];
        view1.alpha=0.4;
        [self.view addSubview:view1];
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        indicatorView.delegate = self;
        indicatorView.numberOfCircles = 3;
        indicatorView.radius = 20;
        indicatorView.internalSpacing = 3;
        indicatorView.center = self.view.center;
        [indicatorView startAnimating];
        [self.view addSubview:indicatorView];
        
        
        NSLog(@"%@",self.usercode.text);
        
        Account *acc= [[AccountTool sharedAccountTool] currentAccount];
        
        
        NSString *url=[xiugaiuserinfo stringByAppendingFormat:@"&username=%@&email=%@&sex=%@&QQ=%@&name=%@&id_card=%@",acc.custPhone,self.email.text,sex,qq,self.name.text,id_card];
        //将url转换一下格式，否则请求的格式就不兼容
        //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
        [HLTNetWorkingtool getary:url success:^(NSArray *ary) {
            
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            if ([ary[0][@"status"] isEqual:@"0"]) {
                //请求成功
                acc.custIdent=id_card;
                acc.Email=self.email.text;
                acc.custName=self.name.text;
                acc.custSex=sex;
                [[AccountTool sharedAccountTool] addAccounts:acc];
                
                
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改资料成功"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                [alert show];
                [self.navigationController popViewControllerAnimated:YES];
                
            }
            else
            {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改资料失败"   message:nil delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
                [alert show];
                
            }
            
        } faliure:^(NSString *error) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改资料失败"   message:@"请求超时" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            
            
        }];
        
    }
    
    
    
    
    
}


//这个是回调方法
#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}








@end
